package com.yanqu.road.server.cmd.activity.gourdchild;

import com.yanqu.road.entity.activity.gourdchild.GourdChildConfig;
import com.yanqu.road.entity.activity.gourdchild.GourdChildUserData;
import com.yanqu.road.pb.activity.GourdChildProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.gourdchild.GourdChildModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.gourdchild.GourdChildMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_GOURD_CHILD_GET_GAME_DATA, desc = "获取游戏数据")
public class GourdChildGetGameDataCmd extends AbstractCommand {

    int passId;
    boolean train = false;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GourdChildProto.GourdChildGetGameDataReqMsg reqMsg = GourdChildProto.GourdChildGetGameDataReqMsg.parseFrom(packet.getBytes());
        if (reqMsg.hasPassId()) {
            passId = reqMsg.getPassId();
        }
        if (reqMsg.hasTrain()){
            train = reqMsg.getTrain();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        GourdChildConfig config = GourdChildMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_GOURD_CHILD_ACTIVITY_NO_OPEN;
            return;
        }
        GourdChildModule module = player.getModule(GourdChildModule.class);
        GourdChildUserData userData = module.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_GOURD_CHILD_ACTIVITY_NO_UNLOCK;
            return;
        }

        ret = player.getModule(GourdChildModule.class).getGameData(passId, train);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            GourdChildProto.GourdChildGetGameDataRespMsg.Builder respBuild = GourdChildProto.GourdChildGetGameDataRespMsg.newBuilder();
            respBuild.setRet(ret);
            player.sendPacket(Protocol.U_GOURD_CHILD_GET_GAME_DATA, respBuild);
        }
    }
}
